package com.sport;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

import com.sport.pojo.Account;
import com.sport.util.MySQLitOpenHelper;
import com.sport.util.ToastUtil;

import java.util.List;

public class RegisterActivity extends AppCompatActivity {
    private EditText et_name, et_pwd, et_email, et_cp;
    private RadioGroup rg_sex;
    private Button btn_register;
    private Button btn_go_login;
    private String name, email, pwd, sex, confirmPwd;
    MySQLitOpenHelper Mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();
        Mydb = new MySQLitOpenHelper(this);

    }

    private void init() {
        et_name = findViewById(R.id.et_name);
        et_pwd = findViewById(R.id.et_pwd);
        et_cp = findViewById(R.id.et_cp);
        et_email = findViewById(R.id.et_email);
        rg_sex = findViewById(R.id.rg_sex);
        btn_register = findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this::onClick);
        btn_go_login = findViewById(R.id.btn_go_login);
        btn_go_login.setOnClickListener(this::onClick);

        rg_sex.setOnCheckedChangeListener(new RadioGroup.
                OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) { //判断被点击的RadioButton
                    case R.id.rb_boy:
                        sex = "男";
                        break;
                    case R.id.rb_girl:
                        sex = "女";
                        break;
                }
            }
        });
    }

    /**
     * 获取界面输入的信息
     */

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                if (getdata()) {
                    Account account = new Account(name, pwd, email, sex);
                    List<Account> accounts = Mydb.selectAccountByName(account);
                    if (accounts.isEmpty()) {
                        //传入数据库
                        long rowID = Mydb.addAccount(account);
                        if (rowID != -1) {
                            ToastUtil.toastShort(this, "注册成功");
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtil.toastShort(this, "注册失败");
                        }
                    } else {
                        ToastUtil.toastShort(this, "用户已存在");
                    }
                }
                break;
            case R.id.btn_go_login:
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private boolean getdata() {
        name = et_name.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
        confirmPwd = et_cp.getText().toString().trim();
        email = et_email.getText().toString().trim();
        // 用户名不能为空
        if (TextUtils.isEmpty(name)) {
            ToastUtil.toastShort(this, "请输入用户名");
            return false;
        }

        // 密码不能为空
        if (TextUtils.isEmpty(pwd)) {
            ToastUtil.toastShort(this, "请输入用户名");
            return false;
        }

        // 确认密码不能为空
        if (TextUtils.isEmpty(confirmPwd)) {
            ToastUtil.toastShort(this, "请再次确认密码");
            return false;
        }

        // 密码和确认密码必须一致
        if (!TextUtils.equals(pwd, confirmPwd)) {
            ToastUtil.toastShort(this, "两次确认的密码不一致");
            return false;
        }

        // 邮箱不能为空
        if (TextUtils.isEmpty(email)) {
            ToastUtil.toastShort(this, "请输入邮箱地址");
            return false;
        }
        // 邮箱格式必须正确
        if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            ToastUtil.toastShort(this, "请输入有效的邮箱地址");
            return false;
        }
        // 性别不能为空
        if (sex == null) {
            ToastUtil.toastShort(this, "请选择性别");
            return false;
        }

        return true;
    }
}